﻿#include "stdafx.h"
#include "script.h"
#include "thread_control.h"
#include "log.h"
#include "scriptMaple.h"
#include "GameDlg.h"
#include "resource.h"
#include "ScriptWls.h"
int gTime = 0;
int nCountTime = 0;
bool isGif = false;
int gnIndex = 0;
unsigned WINAPI SubThread(PVOID pParam);
void CheckException(long index);

// 我们做暂停和恢复操作,就主要靠这个延时函数,要求脚本所有用到延时的地方，全部用这个，这样我们可以有很多机会去暂停线程
void ScriptDelay(long index, long time)
{
	// 判断是否有暂停和结束标记
	if (g_info[index].is_stop)
	{
		// 直接退出当前线程,退出前一定要在本线程内解绑,因为绑定模式0和2在线程外解绑的话会失败,导致解绑失败.
		if (g_info[index].dm)
		{
			g_info[index].dm->UnBindWindow();
		}
		_endthreadex(0);
	}

	if (g_info[index].is_pause)
	{
		g_info[index].thread_state = State_Pause;
		ThreadNotifyUI_Post(NOTIFY_UPDATE, index);

		// 如果你想要在暂停时让用户可以操作,那么可以调用EnableBind,但是不要去调用LockInput,LockInput不是用来解除后台的,具体参考LockInput的说明
		if (g_info[index].dm)
		{
			g_info[index].dm->EnableBind(5);
		}

		// 我们暂停的方法是死循环,然后延时,而不是调用系统的接口
		// 这样开销最小,并且效率也还不错
		while (1)
		{
			if (!g_info[index].is_pause)
			{
				g_info[index].thread_state = State_Runing;
				ThreadNotifyUI_Post(NOTIFY_UPDATE, index);

				// 开启后台
				if (g_info[index].dm)
				{
					g_info[index].dm->EnableBind(1);
				}
				break;
			}

			if (g_info[index].is_stop)
			{
				// 直接退出当前线程,退出前一定要在本线程内解绑,因为绑定模式0和2在线程外解绑的话会失败,导致解绑失败.
				if (g_info[index].dm)
				{
					g_info[index].dm->UnBindWindow();
				}
				_endthreadex(0);
			}

			Sleep(1);
		}
	}

	// 可能暂停,恢复时会让状态错乱,这里再判断一次
	if (g_info[index].thread_state != State_Runing)
	{
		g_info[index].thread_state = State_Runing;
		ThreadNotifyUI_Post(NOTIFY_UPDATE, index);

		// 开启后台
		if (g_info[index].dm)
		{
			g_info[index].dm->EnableBind(1);
		}
	}

	Sleep(time);
}

void SetTaskState(long index, const TCHAR * state)
{
	_tcscpy(g_info[index].task_state, state);
	ThreadNotifyUI_Post(NOTIFY_UPDATE, index);
}

// 脚本主线程,脚本的主要逻辑在这里处理
unsigned WINAPI MainThread(PVOID pParam)
{
	long index = (long)(DWORD_PTR)pParam;
	CDm * dm;
	// 初始化当前线程com组件为MTA模式
	CoInitializeEx(NULL, 0);
	g_info[index].thread_state = State_Runing;
	ThreadNotifyUI_Post(NOTIFY_UPDATE, index);
	// 创建对象
	g_info[index].dm = new CDm;
	
	dm = g_info[index].dm;

	// 检测对象是否创建成功,虽然这个一般不会失败,但为了程序健壮性考虑还是加上,如果内存吃紧，还是可能会失败
	if (dm == NULL || dm->Ver().GetLength() == 0)
	{
		Log(_T("对象创建失败"));
		ThreadNotifyUI_Post(NOTIFY_STOP, index);
		return 0;
	}
	// 开启全局共享字库
	dm->EnableShareDict(1);

	// 其他设置,比如路径等等 当前目录
	long ret = dm->SetPath(_T("c:\\MobileGame\\pic\\"));
	//g_main_cwnd->AppendText(L"绑定图片" + NumberToString(ret));
	dm->SetDict(0, L"jinbi.txt");
	dm->SetDict(1, L"code.txt");
	
	dm->SetShowErrorMsg(0);
	// 开始绑定,主绑定一定要第一个绑定,并且主绑定所在的线程绝对不能结束,否则会造成绑定失效
	//long dm_ret = dm->BindWindowEx(g_info[index].hwnd, _T("normal"), _T("normal"), _T("normal"), _T("dx.public.anti.api"), 0);
	//if (dm_ret != 1)
	//{
	//	Log(_T("主:绑定失败，错误码:%d"), dm->GetLastError());
	//	// 通知主线程进行结束操作(释放资源)
	//	ThreadNotifyUI_Post(NOTIFY_STOP, index);
	//	return 0;
	//}
	// 禁止输入
	//dm->LockInput(4);
	// 我们可以创建副线程了
	// 更新副线程信息
	g_info[index + MAX_HWND].thread_state = State_Starting;
	// 通知UI,副线程开始创建了
	ThreadNotifyUI_Post(NOTIFY_UPDATE, index + MAX_HWND);
	// 创建副线程
	//调式测试
	g_info[index + MAX_HWND].handle = (HANDLE)_beginthreadex(0, 0, SubThread, (PVOID)(DWORD_PTR)(index + MAX_HWND), 0, 0);
	if (g_info[index + MAX_HWND].handle == NULL)
	{
		// 这里必须要解绑,因为模式0和2不在当前线程解绑的话,会导致无法解绑
		dm->UnBindWindow();

		Log(_T("创建副线程失败"));
		// 通知主线程进行结束操作(释放资源)
		ThreadNotifyUI_Post(NOTIFY_STOP, index);
		return 0;
	}
	g_bSwicth = false;
	int count = g_main_cwnd->m_list_info.GetItemCount();
	for (int i = 0; i < count; i++)
	{
		CString strState = g_main_cwnd->m_list_info.GetItemText(i, 6);
		if (strState != L"账号错误" &&
			strState != L"二密错误" &&
			strState != L"账号在线" &&
			strState != L"完成" &&
			strState != L"武器未装备")
		{
			gnIndex = i;
			g_bSwicth = false;
			//开始执行游戏
			//如果当前线路为-1
			int nCodeLine = g_IniFile.GetInt(L"Set", L"CodeLine");
			//g_main_cwnd->m_list_info.GetColumn();

			int nListCodeLine = _wtoi(g_main_cwnd->m_list_info.GetItemText(i, 12));
			if (nListCodeLine == 0) {
				nListCodeLine = _wtoi(g_main_cwnd->m_list_info.GetItemText(i, 11));
			}
			if (nCodeLine == -1) {//首次启动迅游
				g_main_cwnd->AppendText(L"首次启动线路:"+ arrLineCode[1]);
				if (ChangeLine(arrLineCode[1])) {
					g_main_cwnd->AppendText(L"加速成功");
					g_IniFile.SetInt(L"Set", L"CodeLine", 1);
					g_main_cwnd->SetDlgItemTextW(IDC_EDIT3, L"1");
					//g_main_cwnd->GetDlgItem(IDC_STATIC5);
				}
				//取下标为0的线路
				//g_main_cwnd->
				//CListBox* list = (CListBox*)g_main_cwnd->GetDlgItem(IDC_LIST1);
				
				//启动成功后更新配置线路
			}
			else if(nCodeLine != -1 && nCodeLine != nListCodeLine)
			{
				isGif = true;//开启截屏处理
				long hwnd = g_dm->FindWindow(L"MapleStoryClass", L"MapleStory");
				if (hwnd  != 0) {
					g_bSwicth = false;
					KillProcess(L"MapleStory.exe");
					ScriptDelay(index, 1000);
				}
				if (ChangeLine(arrLineCode[nListCodeLine])) {
					g_IniFile.SetInt(L"Set", L"CodeLine", nListCodeLine);
					g_main_cwnd->SetDlgItemTextW(IDC_EDIT3, NumberToString(nListCodeLine));
				}

				//取当前列表线路下标-1
				//根据线路换线
				//启动成功后更新配置线路
			}
			long hwnd = g_dm->FindWindowW(L"XunYouMainClass_4B32", L"迅游国际版");
			if (hwnd > 0) {
				g_dm->SetWindowState(hwnd, 2);
			}
			g_flag = true;
			//g_bSwicth = false;
			//DoWork(index,i,false);
			if (g_main_cwnd->m_ZhaKun.GetCheck() == 1|| g_main_cwnd->m_wlsYou.GetCheck() == 1) {
				g_nState = 0;//初始化扎昆状态
				if (g_main_cwnd->m_ZhaKun.GetCheck() == 1) {
					DoWorkZhaKun(index, i, g_bSwicth);
				}
				if (g_main_cwnd->m_wlsYou.GetCheck() == 1) {
					//DoWorkZhaKun(index, i, g_bSwicth);
					//WlsYouPiao()

					DoWorkWlsYou(index, i, g_bSwicth);
				}

			}
			else {
				//DoWork(index, i, false);
				DoWorkWls(index, i);
			}
			//当前状态未改变
			if (g_AccountFile.GetInt(NumberToString(i + 1), L"state") ==0) {
				i--;
			}
		}
		g_main_cwnd->AppendText(L"当前索引" + NumberToString(i));
		//SetTaskState(index, _T("开始做任务"));
		//SetTaskState(index, _T("任务完成"));
	}
	g_main_cwnd->AppendText(L"任务执行完毕");
	if (g_main_cwnd->m_ZhaKun.GetCheck() == 1) {
		CloseWindows();
	}
	
	//while (1)
	//{

	//}
}

//void DoWorkWlsYou(long index, int taskIndex, bool bLoginState) {
//	if (LoginClientWls(index, taskIndex)) {
//		if (LoginGameWls(index, taskIndex)) {
//			 WlsYouPiao(index, taskIndex);
//		}
//	}
//}

void DoWorkZhaKun(long index, int taskIndex, bool bLoginState) {
	bool flag = false;

	if (LoginClientWls(index, taskIndex)) {
		flag = true;
	}
	
	//状态已完成则返回
	
	if (flag) {
		if (LoginGameWls(index, taskIndex,true)) {
			StartTaskZhaKun(index, taskIndex);
			g_main_cwnd->AppendText(L"已经完成第" + NumberToString(taskIndex) + L"个,换号状态:" + NumberToString(bLoginState));
		}
	}
	if (g_main_cwnd->m_list_info.GetItemText(taskIndex, 6) == L"完成") {
		g_main_cwnd->AppendText(L"当前状态已完成");
		return;
	}
}

void DoWorkWlsYou(long index, int taskIndex, bool bLoginState) {
	if (LoginClientWls(index, taskIndex)) {
		if (LoginGameWls(index, taskIndex)) {
			WlsYouPiao(index, taskIndex);
		}
	}
}

void DoWork(long index, int taskIndex, bool bLoginState)
{
	//登录
	bool flag = false;

	if (bLoginState) {
		if (SwitchCode(index, taskIndex)) {
			flag = true;
		}
	}
	else {
		if (StartLoginClient(index, taskIndex)) {
			flag = true;
		}
	}
	//状态已完成则返回
	if (g_main_cwnd->m_list_info.GetItemText(taskIndex, 6) == L"完成") {
		g_main_cwnd->AppendText(L"当前状态已完成");
		return;
	}
	if (flag) {
		if (StartLoginGame(index, taskIndex, true)) {
			StartTask(index, taskIndex);
		}
	}

	//dmsoft * dm = g_info[index].dm;

	//dm->KeyPressChar(_T("m"));
	//ScriptDelay(index, 500);
}


void SetExcepState(long index, const TCHAR * state)
{
	_tcscpy(g_info[index].excep_state, state);
	ThreadNotifyUI_Post(NOTIFY_STOP, index);
}

// 脚本副线程,用于检测异常等
unsigned WINAPI SubThread(PVOID pParam)
{
	long index = (long)(DWORD_PTR)pParam;

	CDm * dm;

	// 初始化当前线程com组件为MTA模式
	CoInitializeEx(NULL, 0);

	g_info[index].thread_state = State_Runing;
	ThreadNotifyUI_Post(NOTIFY_UPDATE, index);

	// 创建对象
	g_info[index].dm = new CDm;
	dm = g_info[index].dm;

	// 检测对象是否创建成功,虽然这个一般不会失败,但为了程序健壮性考虑还是加上,如果内存吃紧，还是可能会失败
	if (dm == NULL || dm->Ver().GetLength() == 0)
	{
		Log(_T("对象创建失败"));
		SetExcepState(index, _T("对象创建失败"));
		return 0;
	}

	// 开启全局共享字库
	dm->EnableShareDict(1);
	dm->SetShowErrorMsg(0);
	// 其他设置,比如路径等等
	//dm->SetPath(_T("c:\test_game"));
	long ret = dm->SetPath(_T("c:\\MobileGame\\pic\\"));

	//g_info[index].dm->BindWindowEx(g_info[index].hwnd, L"normal", L"normal", L"normal", L"", 0);
	//long dm_ret = dm->BindWindowEx(g_info[index].hwnd, _T("normal"), _T("normal"), _T("dx"), _T("dx.public.anti.api"), 0);
	//if (dm_ret != 1)
	//{
	//	Log(_T("副:绑定失败，错误码:%d"), dm->GetLastError());
	//	// 通知主线程进行结束操作(释放资源)
	//	SetExcepState(index, _T("副:绑定失败"));
	//	return 0;
	//}


	while (1)
	{
		// 检测一些异常,比如突然弹出的对话框，目标窗口被关闭或者掉线等突发情况
		// 比如检测到掉线，可考虑通知UI,然后重新运行
		CheckException(index);
		//ScriptDelay(index, 3000);
	}
}

void CheckException(long index)
{
	CDm * dm = g_info[index].dm;
	long hwnd = g_info[index].dm->FindWindow(L"MapleStoryClass", L"MapleStory");
	if (g_info[index].dm->GetBindWindow() != hwnd && g_info[index].dm->GetBindWindow()!=0) {
		g_info[index].dm->UnBindWindow();
	}
	if (g_info[index].dm->GetBindWindow() == 0 && hwnd!=0) {
		g_info[index].dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"", 0);

	}
	//是否开启检测窗口
	int nTime = 0;
	if (CheckState && g_flag) {
		if (hwnd == 0) {
			g_flag = false;	//窗口不存在则程序中断
			CheckState = false;//检查窗口为假等待下次设定
		}
		if (g_main_cwnd->m_ZhaKun.GetCheck() == 1) {
			nTime = GetTickCount();
			if (dm->IsDisplayDead(0, 760, 14, 769, 1) == 1) {
				//nCountTime++;
				gTime = gTime + (GetTickCount() - nTime) / 1000;
			}
			else {
				gTime = 0;
			}
			if (gTime > 380) {
				//如果当前的配置已经存在则角色为下一角色
				//获取配置账号
				CString strRole = NumberToString(gnIndex) + L"," + g_main_cwnd->m_list_info.GetItemText(gnIndex, 7);
				CString strRole1 = g_IniFile.GetString(NumberToString(gnIndex + 1), L"RoleCode");
				g_main_cwnd->AppendText(L"当前角色:" + strRole + L"配置角色:" + strRole1);
				if (strRole == strRole1) {
					int nRoleNum = g_AccountFile.GetInt(NumberToString(gnIndex + 1), L"RoleNum");
					int nZhaNum = g_AccountFile.GetInt(NumberToString(gnIndex + 1), L"ZhaNum");
					if (nRoleNum == nZhaNum) {//切换账号
						g_main_cwnd->m_list_info.SetItemText(gnIndex, 6, L"完成");
						g_AccountFile.SetString(NumberToString(gnIndex + 1), L"state", L"1");
					}
					else {
						++nRoleNum;
						g_AccountFile.SetInt(NumberToString(gnIndex + 1), L"RoleNum", nRoleNum);
						g_main_cwnd->m_list_info.SetItemText(gnIndex, 7, NumberToString(nRoleNum));
					}
				}
				g_IniFile.SetString(NumberToString(gnIndex + 1), L"RoleCode", NumberToString(gnIndex) + L"," + g_main_cwnd->m_list_info.GetItemText(gnIndex, 7));
				gTime = 0;
				KillProcess(L"MapleStory.exe");
				g_flag = false;
			}
			if (gTime == 370) {
				CString str; 
				str = g_main_cwnd->m_list_info.GetItemText(gnIndex, 1) + g_main_cwnd->m_list_info.GetItemText(gnIndex, 7);
				dm->CaptureGif(0, 0, 1280, 960, L"d:\\pic\\" + str + L".gif", 100, 40000);
			}

			//if (g_nState == 0) {
			//	if (dm->IsDisplayDead(0, 760, 14, 769, 1) == 1) {
			//		//nCountTime++;
			//		gTime = gTime + (GetTickCount() - nTime) / 1000;
			//	}
			//	else {
			//		//nCountTime = 0;
			//		gTime = 0;
			//	}
			//}
			//if (gTime > 200) {//关闭游戏
			//	g_nState = 11;
			//	gTime = 0;
			//	KillProcess(L"MapleStory.exe");
			//	//gTime = 0;
			//	//int nRoleNum = g_AccountFile.GetInt(NumberToString(taskIndex + 1), L"RoleNum");
			//	//int nZhaNum = g_AccountFile.GetInt(NumberToString(taskIndex + 1), L"ZhaNum");
			//	//if (nRoleNum == nZhaNum) {//切换账号
			//	//	g_main_cwnd->m_list_info.SetItemText(taskIndex, 6, L"完成");
			//	//	g_AccountFile.SetString(NumberToString(taskIndex + 1), L"state", L"1");
			//	//}
			//	//else {//执行下一角色
			//	//	g_main_cwnd->m_list_info.SetItemText(taskIndex, 7, NumberToString(nRoleNum));
			//	//	g_AccountFile.SetString(NumberToString(taskIndex + 1), L"ZhaNum", NumberToString(++nZhaNum));
			//	//}
			//	//return true;
			//	//g_flag = true;
			//}
			//g_main_cwnd->AppendText(L"运行状态:" + NumberToString(g_flag) + L"检测状态:" + NumberToString(CheckState));
			g_main_cwnd->AppendText(L"运行状态:" + NumberToString(g_flag) + L"检测状态:" + NumberToString(CheckState) + L"时间间隔" + NumberToString(gTime));

		}
	}
	long x, y;

	if (dm->RunScript(L"二密窗口")) {
		CTime tm; 
		CString str,str1;
		str = tm.Format("%H%M%S");
		str1 = g_main_cwnd->m_list_info.GetItemText(gnIndex, 1) + g_main_cwnd->m_list_info.GetItemText(gnIndex, 7);
		//dm->CaptureGif(234, 242, 789, 496, L"d:\\pic\\" + str+str1 + L".gif", 100, 15000);
	}

	//进入扎昆地图开始截图
	CString str; //获取系统时间 　　
	CTime tm; tm = CTime::GetCurrentTime();
	str = tm.Format("%H%M%S");
	if (isGif) {
		dm->CaptureGif(135, 150, 1239, 768, L"d:\\pic\\line"+str + L".gif", 100, 40000);
		isGif = false;
	}
	if (g_bloodState) {
		//g_main_cwnd->AppendText(L"补血颜色1："+ g_info[index].dm->GetColor(545, 712)+L"补血颜色2"+ dm->GetColor(567, 720));
		if (g_info[index].dm->GetColor(545, 712) == L"f0f0f0") {
			if (g_info[index].dm->GetColor(567, 720) == L"9e9da0") {
				//g_main_cwnd->AppendText(L"补血");
				g_info[index].dm->KeyPress(35);
			}
		}
		else {
			if (g_info[index].dm->GetColor(567, 736) == L"9e9da0") {
				g_main_cwnd->AppendText(L"补血");
				g_info[index].dm->KeyPress(35);
			}
		}
		ScriptDelay(index, 1000);
	}
	ScriptDelay(index, 1000);



	//任务开始时检查窗口是否存在

	//if (dm->CmpColor(579, 717, L"fb6bd1", 0.8) == 1) {
	//	dm->KeyPress(35);
	//	ScriptDelay(index,1000);
	//	//moveKey(35, 300, 200);
	//}


	//// 检测窗口是否存在
	//if (dm->GetWindowState(g_info[index].hwnd, 0) == 0)
	//{
	//	SetExcepState(index, _T("窗口不见了"));
	//}

	// 检测窗口是否卡死



	// 检测是否掉线


	// 检测是否有弹出窗口



	// 其他检测
	//dm->KeyPressChar(_T("s"));
}


bool runScript(long index, CString sOcrText) {
	//获取ocr数据
	//g_main_cwnd->AppendText(L"123");
	OcrInfo ocrInfo = ocrMap[sOcrText];
	//g_main_cwnd->AppendText(ocrInfo.sDescribe);
	ocrInfo.nIndex = index;
	if (ocrInfo.nCount == 0 || ocrInfo.nCount == NULL)
		ocrInfo.nCount = 1;

	bool(*funcOcr[])(OcrInfo) = { CmpCol,FindCol,FindMulCol,FindPic,FindStr,FindColBlock };
	for (int i = 0; i < ocrInfo.nCount; i++)
	{
		if (funcOcr[_wtoi(ocrInfo.sAction)](ocrInfo)) {
			return true;
		}
		ScriptDelay(index, 100);
	}
	return false;
}

void(*funcType[])(OcrInfo) = { dmClick,dmDoubleClick };
bool FindMulCol(OcrInfo ocrInfo) { return true; }
bool CmpCol(OcrInfo ocrInfo) { return true; }

void dmClick(OcrInfo ocrInfo) {
	g_info[ocrInfo.nIndex].dm->LeftClick();
	ScriptDelay(ocrInfo.nIndex, 100);
}
void dmDoubleClick(OcrInfo ocrInfo) {
	g_info[ocrInfo.nIndex].dm->LeftDoubleClick();
	ScriptDelay(ocrInfo.nIndex, 100);
}
bool FindCol(OcrInfo ocrInfo) {
	return true;
}
bool FindPic(OcrInfo ocrInfo) {
	VARIANT x, y;
	VARIANT nPoint[4];
	SplitVARIANT(ocrInfo.sPoint, nPoint, L",");
	CString sBmp = ocrInfo.sDescribe != L"" ? ocrInfo.sDescribe : ocrInfo.sName + L".bmp";
	g_info[ocrInfo.nIndex].dm->FindPic(nPoint[0].lVal - 10, nPoint[1].lVal - 10, nPoint[2].lVal + 10, nPoint[3].lVal + 10, sBmp, L"101010", 0.9f, 0, &x.lVal, &y.lVal);
	g_info[ocrInfo.nIndex].x = x.lVal;
	g_info[ocrInfo.nIndex].y = y.lVal;
	if (x.lVal > 0) {
		if (ocrInfo.sType == L"0")
			return true;
		if (ocrInfo.sOffset != L"") {
			VARIANT nOffset[2];
			SplitVARIANT(ocrInfo.sOffset, nOffset, L",");
			ocrInfo.x = nOffset[0].lVal + x.lVal;
			ocrInfo.y = nOffset[1].lVal + y.lVal;
		}
		else {
			ocrInfo.x = x.lVal;
			ocrInfo.y = y.lVal;
		}
		g_info[ocrInfo.nIndex].dm->MoveTo(ocrInfo.x, ocrInfo.y);
		funcType[_wtoi(ocrInfo.sType) - 1](ocrInfo);
		return true;
	}
	return false;

}
bool FindStr(OcrInfo ocrInfo) { return true; }
bool FindColBlock(OcrInfo ocrInfo) { return true; }
bool runScriptList(long index, CString str) {
	int istart = 0;
	while (istart != -1) {//分割 @符号
		CString m;
		m = str.Tokenize(L"|", istart);
		runScript(index, m);
		Sleep(100);
	}
	return true;
}


void moveKey(long index,int key, int keyDelay) {
	g_info[index].dm->KeyDown(key);
	g_info[index].dm->delay(keyDelay);
	g_info[index].dm->KeyUp(key);
	g_info[index].dm->delay(keyDelay);
}

void CloseWindows() {

	HANDLE hToken;
	TOKEN_PRIVILEGES tkp;
	if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
	{
		
		g_main_cwnd->MessageBox(_T("关机失败..."), _T("警告"), MB_OK);
		return;
	}
	LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
		&tkp.Privileges[0].Luid);
	tkp.PrivilegeCount = 1;
	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
	AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, NULL);
	if (GetLastError() != ERROR_SUCCESS)
	{
		g_main_cwnd->MessageBox(_T("关机失败..."), _T("警告"), MB_OK);
		return;
	}
	ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);

}
void CloseTearm(int index) {
	CDm* dm = g_info[index].dm;
	//如果不存在导航窗口则开始退组，存在导航窗口则关闭导航窗口
	if (dm->RunScript(L"组队血条")) {
		dm->KeyPressChar(L"p");
		ScriptDelay(index, RandomNumber(1000,1100));
		if (dm->RunScript(L"导航窗口")) {
			dm->RunScript(L"退出组队");
		}
		ScriptDelay(index, RandomNumber(1000, 1500));
		if (dm->RunScript(L"导航窗口")) {
			dm->KeyPressChar(L"p");
		}
	}
	else {
		if (dm->RunScript(L"导航窗口")) {
			dm->RunScript(L"退出组队");
		}
	}

		
}

void RunScriptList(int index,CString strList) {
	CDm* dm = g_info[index].dm;
	CStringArray strArr;
	StringSplit(strList, strArr, '|');
	for (size_t i = 0; i < strArr.GetCount(); i++)
	{
		dm->RunScript(strArr.GetAt(i));
	}
}

void ChoseBoss(int index, CString strBoss, int offX, int offY) {
	CDm* dm = g_info[index].dm;
	long x, y;
	dm->KeyPressChar(L"t");
	ScriptDelay(index, RandomNumber(500, 1000));
	dm->RunScript(strBoss, &x, &y);
	ScriptDelay(index, RandomNumber(1000, 1500));
	dm->MoveToEx(x + offX, y + offY, 10, 10);
	dm->LeftClick();
}
//退出祭坛
void ExitZkjt(int index) {
	CDm* dm = g_info[index].dm;
	if (dm->RunScript(L"扎昆祭台入口")) {
		dm->KeyDownChar(L"left");
		Sleep(200);
		for (size_t i = 0; i < 100; i++)
		{
			dm->KeyPressChar(L"up");
		}
		dm->KeyUpChar(L"left");
		Sleep(RandomNumber(2000, 2500));
		if (!dm->RunScript(L"扎昆祭台入口")) {
			return;
		}
		dm->KeyDownChar(L"right");
		Sleep(200);
		for (size_t i = 0; i < 100; i++)
		{
			dm->KeyPressChar(L"up");
		}
		dm->KeyUpChar(L"right");
	}
}


//查找电脑百分比
bool FindComputer(int index,int taskIndex) {
	CDm* dm = g_info[index].dm;
	long x, y;

	if (dm->RunScript(L"红电脑")) {
		g_main_cwnd->m_list_info.SetItemText(taskIndex, 10, L"红电脑");
		return true;
	}
	if (!dm->RunScript(L"设置")) {
		dm->KeyPressChar(L"esc");
	}
	Sleep(RandomNumber(1000, 1500));
	if (dm->RunScript(L"设置",&x,&y)) {
		dm->MoveToEx(x + 15, y + 90, 10, 3);
		dm->LeftClick();
		Sleep(RandomNumber(1000, 1500));
		if (dm->RunScript(L"电脑列表")) {
			Sleep(RandomNumber(500, 1000));
			if (dm->RunScript(L"电脑百分比")) {
				g_main_cwnd->m_list_info.SetItemText(taskIndex, 10, L"50%-");
				Sleep(RandomNumber(1000, 1500));
				if (dm->RunScript(L"电脑列表")) {
					dm->KeyPressChar(L"esc");
					return true;
				}
			}
		}
		Sleep(RandomNumber(1000, 1500));
		if (dm->RunScript(L"电脑列表")) {
			dm->KeyPressChar(L"esc");
			return true;
		}
	}
	return false;
}

void ExitZkrk(int index) {
	CDm* dm = g_info[index].dm;
	long x, y, x1, y1;
	if (dm->RunScript(L"扎昆入口")) {
		ShowLog(L"开始退出扎昆地图");
		dm->RunScript(L"小地图", &x, &y);
		dm->RunScript(L"人物坐标", &x1, &y1);
		if (movetoX(index, RandomNumber(138,141))) {
			for (size_t k = 0; k < 5; k++)
			{
				dm->RunScript(L"npc啊杜比斯");
				dm->RunScript(L"退出扎昆");
				if (dm->RunScript(L"下一步")) {
					dm->KeyPressChar(L"space");
				}

			}
		}
		//dm->KeyDownChar(L"right");
		//for (size_t i = 0; i < 10; i++)
		//{
		//	if (x1 - x < 116) {//未到达阿们npc
		//		Sleep(RandomNumber(500, 1000));
		//	}
		//	else {
		//		dm->KeyUpChar(L"right");
		//		break;
		//	}
		//	
		//}
		//dm->KeyUpChar(L"right");
	}
}


bool bossAttack(int index, int taskIndex) {
	long x, y, k = 0;
	for (size_t i = 0; i < 15; i++)
	{
		//g_main_cwnd->AppendText(g_info[index].dm->GetColor(137, 13));
		if (runScript(index, L"bossEnd")) {//boss结束
			ScriptDelay(index, 1000);
			return true;
		}
		for (size_t i = 0; i < 5; i++)
		{
			g_info[index].dm->FindColorBlock(0, 228, 1024, 580, L"292222-101010", 1, 2000, 150, 50, &x, &y);
			if (g_info[index].x > 0) {
				x = g_info[index].x;
				break;
			}
			if (x > 0) {
				OutPutListView(taskIndex, L"boss坐标" + NumberToString(x));

				break;
			}

			ScriptDelay(index, 100);
		}
		if (x > 521 && x < 850)
		{
			//往右直接攻击
			k = 1;
			OutPutListView(taskIndex, L"往右直接攻击,坐标:" + NumberToString(x));
			Attack(index, 39);
			i--;
			continue;
		}
		else if (x > 850) {
			k = 1;
			OutPutListView(taskIndex, L"往右移动攻击,坐标:" + NumberToString(x));
			MoveAttack(index, 39);
			i--;
			continue;
		}
		else if (x > 150 && x < 500) {
			k = 0;
			OutPutListView(taskIndex, L"往左攻击,坐标:" + NumberToString(x));
			Attack(index, 37);
			i--;
			continue;
		}
		else if (x < 150 && x>0) {
			k = 0;
			OutPutListView(taskIndex, L"往左移动攻击,坐标:" + NumberToString(x));
			MoveAttack(index, 37);
			i--;
			continue;
		}
		if (k == 1) {
			i = 8;
			k = 0;
		}
		if (i <= 7) {

			OutPutListView(taskIndex, L"往左跳");
			JumpAttack(index, 37, g_info[index].key);
		}
		else if (i<15 && i>7) {
			OutPutListView(taskIndex, L"往右跳");
			JumpAttack(index, 39, g_info[index].key);
		}
	}
	return false;
}

void Attack(long index, int key) {
	long x, y;
	g_info[index].dm->KeyPress(key);
	g_info[index].dm->delay(100);
	g_info[index].dm->KeyPress(key);
	g_info[index].dm->delay(100);
	g_info[index].dm->KeyUp(key);
	for (size_t i = 0; i < 5; i++)
	{
		ScriptDelay(index, 200);
		g_info[index].dm->KeyDown(16);
		g_info[index].dm->delay(200);
		g_info[index].dm->KeyUp(16);
		ScriptDelay(index, 200);
		g_info[index].dm->FindColorBlock(0, 228, 1024, 580, L"292222-101010", 1, 3000, 150, 50, &x, &y);
		g_info[index].x = x;
	}
}

void MoveAttack(long index, int key) {
	long x, y;
	g_info[index].dm->KeyDown(key);
	g_info[index].dm->delay(2000);
	g_info[index].dm->KeyUp(key);
	g_info[index].dm->delay(500);
	for (size_t i = 0; i < 5; i++)
	{
		ScriptDelay(index, 200);
		g_info[index].dm->KeyDown(16);
		g_info[index].dm->delay(200);
		g_info[index].dm->KeyUp(16);
		ScriptDelay(index, 200);
		g_info[index].dm->FindColorBlock(0, 228, 1024, 500, L"292222-101010", 1, 3000, 150, 50, &x, &y);
		g_info[index].x = x;
	};
}

void JumpAttack(long index, int key, int mode) {
	long x, y;
	g_info[index].dm->KeyDown(key);
	g_info[index].dm->delay(100);
	g_info[index].dm->KeyPress(mode);//18alt跳 17 ctrl
	g_info[index].dm->delay(100);
	g_info[index].dm->KeyPress(mode);//18alt跳 17 ctrl
	g_info[index].dm->KeyUp(key);
	g_info[index].dm->delay(500);
	g_info[index].dm->FindColorBlock(0, 228, 1024, 580, L"292222-101010", 1, 3000, 150, 50, &x, &y);
	g_info[index].x = x;
}